package com.zjitc.service.impl;

import com.zjitc.common.ServerResponse;
import com.zjitc.dao.UserDao;
import com.zjitc.model.User;
import com.zjitc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/4/17 17:06
 * @description:
 */
@Service
public class UserServiceImpl implements UserService {

  @Autowired
  private UserDao userDao;

  @Override
  public ServerResponse login(String username, String password) {
    int count = userDao.isNotUsername(username);
    if (count != 1){
      return ServerResponse.createError("密码不正确");
    }else {
      User user = userDao.findByUsernameAndPassword(username, password);
      return ServerResponse.createSuccess(user);
    }
  }

  @Override
  public ServerResponse echoInfo(String username) {
    User user = userDao.findByUsername(username);
    if (user == null){
        return ServerResponse.createError("请输入正确的用户名");
    }
    return ServerResponse.createSuccess(user);
  }

  @Override
  public ServerResponse<User> updateInfo(User user){
    int count = userDao.updateInfo(user.getUsername(),user.getPassword(),user.getPhone(),user.getAddress(),user.getEmail());
    if (count > 0){
      return ServerResponse.createSuccess("更新成功");
    }
    return ServerResponse.createError("更新失败");
  }

  @Override
  public ServerResponse<User> forgetPassword(String username) {
    int count = userDao.isNotUsername(username);
    if (count == 1) {
      String email = userDao.userEmail(username);
      return ServerResponse.createError("请前往邮箱修改密码");
    }else {
      return ServerResponse.createError("未找到此用户，请检查用户名是否正确");
    }
  }

  @Override
  public User findUserByUsername(String username) {
    User user = userDao.findByUsername(username);
    return user;
  }

  @Override
  public ServerResponse amendPassword(String password, String uuid) {
    int count = userDao.updatePasswordByUuid(password,uuid);
    return ServerResponse.createError("");
  }

  @Override
  public ServerResponse findUUID(String username,String uuid) {
    int count = userDao.updateUUID(username, uuid);
    return ServerResponse.createError("");
  }

  @Override
  public ServerResponse register(User user) {
    int count=userDao.isNotUsername(user.getUsername());
    if(count==1){
      return ServerResponse.createError("用户已存在");
    }
    int count1 = userDao.register(user);
    return ServerResponse.createSuccess("校验成功");
  }

}
